//
//  YMStatusOriginalView.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/24.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit
import SnapKit




class YMStatusOriginalView: UIView {
    var originalBottomConst:Constraint?
    let df = NSDateFormatter()
    var statusViewModel:YMStatusViewModel?{
        
        didSet{
            
            headImageView.sh_setImage(statusViewModel?.status?.user?.profile_image_url, placeholderImgName: "avatar_default")
            nameLabel.text = statusViewModel?.status?.user?.name
            statusViewModel?.status?.created_at
            
            timeLabel.text = statusViewModel?.timeStr
            sourceLabel.attributedText = statusViewModel?.soucreAttr
            memberImageView.image = statusViewModel?.memberImage
            avatarImageView.image = statusViewModel?.avatarImage
            
            contentLabel.text = statusViewModel?.status?.text
            /*
             配图赋值及逻辑
        有配图:
             首先要卸载底部约束
             刷新约束
             显示配图
        没配图:
             刷新约束
             影藏配图
             */
            
            //先卸载约束
            self.originalBottomConst?.uninstall()
            
            if statusViewModel?.status?.pic_urls?.count > 0 {
                //要先赋值 "传值"
                pictureView.picuserInfo = statusViewModel?.status?.pic_urls
                //代表有配图
//                重新给约束
                self.snp_makeConstraints(closure: { (make) in
                 self.originalBottomConst = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                pictureView.hidden = false
                
            }else{
                //没有配图
//                重新约束
                
                self.snp_makeConstraints(closure: { (make) in
                    self.originalBottomConst = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                pictureView.hidden = true
                
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //        MARK: - 设置视图
    
    func setupUI() {
        //添加控件
        
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        
        
        //添加约束
        //01
        headImageView.snp_makeConstraints { (make) in
            make.top.leading.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
                
        }
        
        //02
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
        }
        
        //03
        memberImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        
        //04
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        //05
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        
        //06
        avatarImageView.snp_makeConstraints { (make) in
            make.centerY.equalTo(headImageView.snp_bottom)
            make.centerX.equalTo(headImageView.snp_trailing)
            
        }
        
        //07
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.leading.equalTo(headImageView)
            make.width.equalTo(SCREENW - 20)
        }
        //08
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo((SCREENW-250)/2)
        }
        
        //当前view做约束
        self.snp_makeConstraints { (make) in
           self.originalBottomConst = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
    }
    
    
    
    
    
    //        MARK: - 懒加载控件
    
    //t头像
    private lazy var headImageView :UIImageView = UIImageView(imgName: "avatar_default")
    //n昵称
    private lazy var nameLabel :UILabel = {
       let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(15)
        lab.textColor = UIColor.blackColor()
        return lab
    }()
    //微博等级
    private lazy var memberImageView:UIImageView = UIImageView(imgName: "common_icon_membership")
    //微博时间
    private lazy var timeLabel:UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(10)
        lab.textColor = ThemeColor
        return lab
    }()
    //认证logo
    private lazy var  avatarImageView:UIImageView = UIImageView(imgName: "")
    //微博来源
    private lazy var sourceLabel:UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(10)
        lab.textColor = ThemeColor
        return lab
    }()
    //微博内容
    private lazy var contentLabel :UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(15)
        lab.textColor = UIColor.darkGrayColor()
        lab.preferredMaxLayoutWidth = SCREENW - 20
        lab.numberOfLines = 0
        return lab
    }()
    //配图view
    private lazy var pictureView:YMStatusPictureView = YMStatusPictureView()
}


////        MARK: - 时间的格式或输出
//
//extension YMStatusOriginalView{
//    
//    func getTimeString(created_at:String,df:NSDateFormatter) ->String{
//        
//       return NSDate().getSinaTimeStr(created_at, df: df)
//    }
//}



//MARK: 正则
extension YMStatusOriginalView{
    func dealContentText(text:String?) {
        
        guard let t = text else{
            return
        }
        
        let regex = "\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]"
        //遍历字符串
        /*
         1.匹配的个数
         2.匹配的而结果
         3.匹配的范围
         4.匹配是否停止
         */
        (t as NSString).enumerateStringsMatchedByRegex(regex) { (count, string, range, _) in
            let emoticon = YMEmoticonTools.sharedTools.searchEmticons((string.memory as? String))
        }
//        print(emoticon?.png)
        
    }
    
}








